<?php
class Item extends AppModel {
	var $name = 'Item';
	var $displayField = 'name';
	//The Associations below have been created with all possible keys, those that are not needed can be removed


	var $belongsTo = array(
		'Category' => array(
			'className' => 'Category',
			'foreignKey' => 'category_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
			),
		'Type' => array(
			'className' => 'Type',
			'foreignKey' => 'type_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
			),
		'Origin' => array(
			'className' => 'Origin',
			'foreignKey' => 'origin_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
			),
		'Supplier' => array(
			'className' => 'Supplier',
			'foreignKey' => 'supplier_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
			),
		'Ownership' => array(
			'className' => 'Ownership',
			'foreignKey' => 'ownership_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
			)
			);

			var $hasMany = array(
		'Disposal' => array(
			'className' => 'Disposal',
			'foreignKey' => 'item_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
			),
		'Inspection' => array(
			'className' => 'Inspection',
			'foreignKey' => 'item_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
			),
		'Location' => array(
			'className' => 'Location',
			'foreignKey' => 'item_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
			),
			/*
		'Upload' => array(
			'className' => 'Upload',
			'foreignKey' => 'item_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
			)*/			
			);

			public function generateSerialNo($id) {
				while(strlen($id) < 6) {
					$id .= '0'.$id;
				}
				$serialno = "KDN/PDRM/SMS/CK/PR/09/H/11/$id";
				return $serialno;
			}
			
			
			function afterFind($results) {
				foreach ($results as $key => $val) {
						
					if (isset($val['Item']['received_date'])) {
						$results[$key]['Item']['received_date'] = $this->dateFormatAfterFind($val['Item']['received_date']);

					}
						
					if (isset($val['Item']['warranty_period'])) {
						$results[$key]['Item']['warranty_period'] = $this->dateFormatAfterFind($val['Item']['warranty_period']);

					}
					
					if (isset($val['Item']['ownership_date'])) {
						$results[$key]['Item']['ownership_date'] = $this->dateFormatAfterFind($val['Item']['ownership_date']);

					}

				}

				return $results;
			}

			function dateFormatAfterFind($dateString) {
				return date('d-M-Y', strtotime($dateString));
			}
			/*
			private function isWarantyExpired($item_id) {
				//todo function 'penanda' return true / false
				echo $this->Text->highlight($item['Item']['warranty_period'],$item_id);

				//return false;
			}*/

}


